package com.monise.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import com.monise.dao.DAO.AnimalDAO;
import com.monise.dao.DAO.ClienteDAO;
import com.monise.dao.DAO.ConsultaServicoDao;
import com.monise.dao.DAO.DadosConsultaDAO;
import com.monise.dao.util.ConsultaServicoTableModel;
import com.monise.model.Animal;
import com.monise.model.Cliente;
import com.monise.model.ConsultaServico;
import com.monise.model.DadosConsulta;
import java.awt.Font;
import java.util.List;

public class FrameConsultaHistorico extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static JTextField txtCodAnimal;
	public static JTextField txtCodResponsavel;
	private JTextField txtNome;
	private JTextField txtCodigo;
	private JTextField txtLaco;
	private JTextField txtGravata;
	private JTextField txtUnha;
	private JTable table;
	private JTextField txtValor;
	private JRadioButton rdAnimal;
	private JButton btnConsultaAnimal;
	private JButton btnConsultaResponsavel = new JButton("");
	private JRadioButton rdResponsavel;
	private JButton btnConsultar;
	private DadosConsulta consultaSelecionada;
	private JTextArea txtDescricao;
	private JCheckBox ckLaco;
	private JCheckBox ckGravata;
	private JCheckBox ckUnha;
	private ConsultaServicoTableModel model;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameConsultaHistorico frame = new FrameConsultaHistorico();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameConsultaHistorico() {
		setTitle("Hist\u00F3rico consulta");
		setClosable(true);
		setBounds(100, 100, 824, 636);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Pesquisa", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		rdAnimal = new JRadioButton("Pesquisar por nome ANIMAL");

		JLabel label = new JLabel("Cod. Animal:");

		txtCodAnimal = new JTextField();
		txtCodAnimal.setColumns(10);

		btnConsultaAnimal = new JButton("");
		btnConsultaAnimal.setIcon(new ImageIcon(FrameConsultaHistorico.class
				.getResource("/com/monise/imagens/pesquisa.png")));

		JLabel label_1 = new JLabel("Cod. Respons\u00E1vel:");

		txtCodResponsavel = new JTextField();
		txtCodResponsavel.setColumns(10);

		btnConsultaResponsavel = new JButton("");
		btnConsultaResponsavel.setIcon(new ImageIcon(
				FrameConsultaHistorico.class
						.getResource("/com/monise/imagens/pesquisa.png")));

		rdResponsavel = new JRadioButton("Pesquisar por nome RESPONS\u00C1VEL");

		btnConsultar = new JButton("Consultar");
		btnConsultar.setIcon(new ImageIcon(FrameConsultaHistorico.class.getResource("/com/monise/imagens/View.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(rdAnimal))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(19)
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtCodAnimal, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnConsultaAnimal)))
					.addGap(30)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtCodResponsavel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnConsultaResponsavel)
							.addPreferredGap(ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
							.addComponent(btnConsultar)
							.addGap(50))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(rdResponsavel)
							.addContainerGap(301, Short.MAX_VALUE))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnConsultar)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(rdAnimal)
								.addComponent(rdResponsavel))
							.addGap(31)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
									.addComponent(label)
									.addComponent(txtCodAnimal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_1)
									.addComponent(txtCodResponsavel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addComponent(btnConsultaAnimal)
								.addComponent(btnConsultaResponsavel))))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Dados consulta", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(20))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 145, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 406, GroupLayout.PREFERRED_SIZE)
					.addGap(27))
		);

		JLabel lblNome = new JLabel("Nome:");

		txtNome = new JTextField();
		txtNome.setEditable(false);
		txtNome.setColumns(10);

		JLabel lblCdigo = new JLabel("C\u00F3digo:");

		txtCodigo = new JTextField();
		txtCodigo.setEditable(false);
		txtCodigo.setColumns(10);

		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o:");

		txtDescricao = new JTextArea();
		txtDescricao.setEditable(false);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Acess\u00F3rios", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		ckLaco = new JCheckBox("La\u00E7o");
		ckLaco.setEnabled(false);

		txtLaco = new JTextField();
		txtLaco.setEditable(false);
		txtLaco.setColumns(10);

		ckGravata = new JCheckBox("Gravata");
		ckGravata.setEnabled(false);

		txtGravata = new JTextField();
		txtGravata.setEditable(false);
		txtGravata.setColumns(10);

		ckUnha = new JCheckBox("Unha");
		ckUnha.setEnabled(false);

		txtUnha = new JTextField();
		txtUnha.setEditable(false);
		txtUnha.setColumns(10);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3
				.setHorizontalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 258, Short.MAX_VALUE)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addComponent(
																				ckLaco)
																		.addGap(18)
																		.addComponent(
																				txtLaco))
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addComponent(
																				ckGravata)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				txtGravata,
																				GroupLayout.PREFERRED_SIZE,
																				155,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addComponent(
																				ckUnha)
																		.addGap(18)
																		.addComponent(
																				txtUnha)))
										.addContainerGap(18, Short.MAX_VALUE)));
		gl_panel_3
				.setVerticalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 172, Short.MAX_VALUE)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(ckLaco)
														.addComponent(
																txtLaco,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(ckGravata)
														.addComponent(
																txtGravata,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(ckUnha)
														.addComponent(
																txtUnha,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(30, Short.MAX_VALUE)));
		panel_3.setLayout(gl_panel_3);

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(null, "Servi\u00E7os realizados",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JLabel lblValor = new JLabel("Valor");
		lblValor.setFont(new Font("Tahoma", Font.BOLD, 12));

		txtValor = new JTextField();
		txtValor.setEditable(false);
		txtValor.setColumns(10);
		
		JLabel lblR = new JLabel("R$");
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 570, GroupLayout.PREFERRED_SIZE)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_2.createSequentialGroup()
									.addGap(10)
									.addComponent(lblR)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel_2.createSequentialGroup()
									.addGap(44)
									.addComponent(lblValor))))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
								.addComponent(lblDescrio)
								.addComponent(lblNome))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel_2.createSequentialGroup()
									.addComponent(txtNome, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
									.addGap(18)
									.addComponent(lblCdigo)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(txtCodigo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addComponent(txtDescricao))
							.addGap(46)
							.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE)))
					.addGap(34))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(25)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNome)
								.addComponent(txtNome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblCdigo)
								.addComponent(txtCodigo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(26)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblDescrio)
								.addComponent(txtDescricao, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(41)
							.addComponent(lblValor)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblR)
								.addComponent(txtValor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(25, Short.MAX_VALUE))
		);

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(gl_panel_4.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_4
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE,
								526, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(22, Short.MAX_VALUE)));
		gl_panel_4.setVerticalGroup(gl_panel_4.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_4
						.createSequentialGroup()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE,
								114, Short.MAX_VALUE).addContainerGap()));

		table = new JTable();
		scrollPane.setViewportView(table);
		panel_4.setLayout(gl_panel_4);
		panel_2.setLayout(gl_panel_2);
		getContentPane().setLayout(groupLayout);

		btnConsultaAnimal.addActionListener(new AbrirListaAnimal());
		btnConsultaResponsavel.addActionListener(new AbrirListaCliente());
		btnConsultar.addActionListener(new AbrirListaConsulta());
		
		limparCampos();

	}

	private static void AbrirTela(JInternalFrame tela) {
		FrameSistema.getInstancia().plPrincipal.add(tela);
		int lDesk = FrameSistema.getInstancia().plPrincipal.getWidth();
		int aDesk = FrameSistema.getInstancia().plPrincipal.getHeight();
		int lIFrame = tela.getWidth();
		int aIFrame = tela.getHeight();

		tela.setLocation(lDesk / 2 - lIFrame / 2, aDesk / 2 - aIFrame / 2);
		tela.setVisible(true);
	}

	private class AbrirListaAnimal implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			FrameTabelaAnimal tela = new FrameTabelaAnimal(1);
			AbrirTela(tela);
			tela.setVisible(true);
		}
	}

	private class AbrirListaCliente implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			FrameTabelaResponsavel tela = new FrameTabelaResponsavel(1);
			AbrirTela(tela);
			tela.setVisible(true);
		}
	}
	
	private class AbrirListaConsulta implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			TabelaHistorico tela;
			
			if(rdAnimal.isSelected()){
				AnimalDAO animalDao = new AnimalDAO();
				Animal animal = animalDao.getAnimal(Long.valueOf(txtCodAnimal
						.getText()));

			 tela = new TabelaHistorico(animal, null,FrameConsultaHistorico.this);
			}
			else{
				ClienteDAO clienteDao = new ClienteDAO();
				Cliente cliente = clienteDao.findById(Long.valueOf(txtCodResponsavel.getText()));
			    tela = new TabelaHistorico(null, cliente,FrameConsultaHistorico.this);
			}
			
			AbrirTela(tela);
			tela.setVisible(true);
		}
	}

	public DadosConsulta getConsultaSelecionada() {
		return consultaSelecionada;
	}

	public void setConsultaSelecionada(DadosConsulta consultaSelecionada) {
		this.consultaSelecionada = consultaSelecionada;
	}

	public void preencherDados() {

		limparCampos();

		if (consultaSelecionada != null) {
			txtNome.setText(consultaSelecionada.getAnimal().getNome());
			txtDescricao.setText(consultaSelecionada.getDescricao());
			txtValor.setText(Double.toString(consultaSelecionada.getValor()));
			txtCodigo.setText(String.valueOf(consultaSelecionada.getPkDadosConsulta()));

			verificaAcessorios();
			preencherTabelaServico();
		}
	}

	private void verificaAcessorios() {
		if (consultaSelecionada.getGravata() != null) {
			txtGravata.setText(consultaSelecionada.getGravata());
			ckGravata.setSelected(true);
		}

		if (consultaSelecionada.getLaco() != null) {
			txtLaco.setText(consultaSelecionada.getLaco());
			ckLaco.setSelected(true);
		}

		if (consultaSelecionada.getUnha() != null) {
			txtUnha.setText(consultaSelecionada.getUnha());
			ckUnha.setSelected(true);
		}
	}

	private void limparCampos() {
		txtGravata.setText("");
		ckGravata.setSelected(false);
		txtLaco.setText("");
		ckLaco.setSelected(false);
		txtUnha.setText("");
		ckUnha.setSelected(false);
		
		txtCodAnimal.setText("");
		txtCodigo.setText("");
		txtCodResponsavel.setText("");
		txtDescricao.setText("");
		txtNome.setText("");
		txtValor.setText("");
	}
	
	private void preencherTabelaServico(){
		ConsultaServicoDao consultaServicoDao = new ConsultaServicoDao();
	    DadosConsultaDAO consultaDAO = new DadosConsultaDAO();
	    DadosConsulta consulta = new DadosConsulta();
	    
	    consulta = consultaDAO.getDadosConsulta(consultaSelecionada.getPkDadosConsulta());
		
		List<ConsultaServico> servicos = consultaServicoDao.getConsultasServico(consulta);
		model = new ConsultaServicoTableModel(servicos);
		
		table.setModel(model);
	}
}
